package com.zhuoyue.zhtj.wx.web;

import com.eit.eitsite.common.web.BaseController;
import com.google.common.collect.Maps;
import com.zhuoyue.zhtj.wx.service.WxCheckService;
import com.zhuoyue.zhtj.wx.util.CheckUtil;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by kaygreen on 2017/9/15.
 */
@Controller
@RequestMapping(value = "/zhtj/wxxx")
public class WxCheckController extends BaseController {
    @Autowired
    WxCheckService wxCheckService;
    @RequestMapping(value = "handRequest")
    protected void handGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String signature = request.getParameter("signature");
        String timestamp = request.getParameter("timestamp");
        String nonce = request.getParameter("nonce");
        String echostr = request.getParameter("echostr");
        PrintWriter out = response.getWriter();
        if(CheckUtil.checkSignature(signature, timestamp, nonce)){
            out.print(echostr);
        }
//        response.getWriter().append("Served at: ").append(request.getContextPath());
    }

//    @RequestMapping(value = "handRequest")
//    protected String handPost(HttpServletRequest request, HttpServletResponse response) throws IOException{
//        Map<String, String> map = Maps.newHashMap();
//        // 从request中取得输入流
//        InputStream inputStream = request.getInputStream();
//        // 读取输入流
//        SAXReader reader = new SAXReader();
//        Document document = null;
//        try {
//            document = reader.read(inputStream);
//        } catch (DocumentException e) {
//            e.printStackTrace();
//        }
//        // 得到xml根元素
//        Element root = document.getRootElement();
//        // 得到根元素的全部子节点
//
//        List<Element> elementList = root.elements();
//        // 遍历全部子节点
//        for (Element e : elementList)  {
//            map.put(e.getName(), e.getText());
//        }
//        if(wxCheckService.handEvent(map)){
//            return "redirect:szptApp/html/broaadcast.html";
//        }
//        // 释放资源
//        inputStream.close();
//        inputStream = null;
//        return null;
//}
}
